package com.glory.brepda.activity.mfg;


import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.DefectCodeEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.ScrapEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DSWorkEndActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvEqpWork)
    TextView tvEqpWork;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.etSliceNum)
    FreeUI_ClearEditText etSliceNum;
    @BindView(R.id.etSliceCode)
    TextView etSliceCode;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.tvPauseStatus)
    TextView tvPauseStatus;
    @BindView(R.id.lotScan)
    ImageView lotScan;
    private static final int REQUEST_CODE_TEXT1 = 1001;
    private List<String> defectCodeList;
    private FreeUI_GeneralPop codePop;
    private LotInfoEntity lotInfoEntity;
    private List<String> mDurablelist;
    private String subqty = "";
    //批次数量
    private String mainqty;
    private FreeUI_GeneralFragmentDialog scanDialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_rowend;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        defectCodeList = new ArrayList<>();

        initEdit(etLot, s -> {
            LotUtils.searchLotInfo(DSWorkEndActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {

                @Override
                public void onSuccess(LotInfoEntity response) {
                    lotInfoEntity = response;
                    etSliceCode.setText(response.getLOT().getCUSTOMERLOTID());
                    tvLotState.setText(response.getLOT().getSTATE());
                    tvStep.setText(response.getLOT().getSTEPNAME());
                    tvEqpWork.setText(response.getLOT().getEQUIPMENTID());
                    subqty = lotInfoEntity.getLOT().getSUBQTY();
                    tvPauseStatus.setText(response.getLOT().getHOLDSTATE());
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });

        });

    }

    private void getDefectInfo(String s) {

        FreeUI_EntityCallBack<BaseResponseBean<DefectCodeEntity>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<DefectCodeEntity>>
                (new TypeToken<BaseResponseBean<DefectCodeEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<DefectCodeEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    defectCodeList.clear();
                    List<DefectCodeEntity.DefectCode> scrapcodelists = response.body().getResponse().getBody().getADREFLIST();
                    for (DefectCodeEntity.DefectCode code : scrapcodelists){
                        defectCodeList.add(code.getKEY() + "/" + code.getDESCRIPTION());
                    }

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<DefectCodeEntity>> response) {
                super.onError(response);
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("REFERENCENAME", Constant.REFERENCENAME);
//        map.add("REFERENCETYPE", new JsonParser().parse(new Gson().toJson(new LotList(s))).getAsJsonObject());//传批次集合
        map.addProperty("REFERENCETYPE","USER");
        NetUtils.requestPostNet("IF.ADREFLISTQUERY", "Common", DSWorkEndActivity.this, map, callback);
    }

    @Override
    protected void initData() {

        lotScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startQrScanner(REQUEST_CODE_TEXT1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && data != null) {
            String qrResult = data.getStringExtra("SCAN_RESULT"); // 获取扫码结果
            if (qrResult != null) {
                switch (requestCode) {
                    case REQUEST_CODE_TEXT1:
                        etLot.setText(qrResult);
                        break;
                }
            }

        }
    }


    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            // M190300401
            //M190300402
            //M190300403
            //M190300404
            //ATM001
            case R.id.tvBaseSubmit:
//                if (actionControl("Submit02")) {
//                    return;
//                }
                if (getText(etLot).equals("")){
                    showToast("请先扫描批次信息",false);
                    return;
                }

                if (tvLotState.getText().toString().equals("待投产")){
                    showConfirmDialog("批次未投产，请联系计划部执行投产");
                    return;
                }

                if (tvLotState.getText().toString().equals("等待作业")){
                    showConfirmDialog("批次没有作业，请操作进站功能");
                    return;
                }

                LotUtils.lotDAWBTrackOut(this, "DSLOTTRACKOUT",getSupportFragmentManager(),"", tvEqpWork.getText().toString(),
                        getText(etLot), null,null,"",subqty, lotInfoEntity, 0, "", null,new LotUtils.netCallback() {
                            @Override
                            public void onSuccess(Object response) {

                            }

                            @Override
                            public void onFail(Throwable t) {

                            }
                        });
                break;


        }

    }
}